<template>
  <div></div>
</template>

<script setup>
//拿到路由中的动态参数
import { onMounted, inject, onBeforeUnmount } from 'vue'
import { useRoute } from 'vue-router'
import { onBeforeRouteUpdate } from 'vue-router'
import { DrawLine, DrawRect, DrawPolygon, DrawCircle } from '@antv/l7-draw'
const { scene } = inject('mapScene')
let route = useRoute()
let draw
//初次加载
onMounted(() => {
  let type = route.params.type
  initDraw(type)
})
//组件切换(路由参数改变)
onBeforeRouteUpdate((to) => {
  let type = to.params.type
  initDraw(type)
})
function initDraw(type) {
  if (draw) {
    draw.destroy()
    draw.clear()
    draw = null
  }
  switch (type) {
    case 'line': {
      draw = new DrawLine(scene, {
        distanceOptions: {},
      })
      break
    }
    case 'rect': {
      draw = new DrawRect(scene, {
        areaOptions: {},
      })
      break
    }
    case 'polygon': {
      draw = new DrawPolygon(scene, {
        areaOptions: {},
      })
      break
    }
    case 'circle': {
      draw = new DrawCircle(scene, {
        areaOptions: {},
      })
      break
    }
  }
  draw.enable()
}
onBeforeUnmount(() => {
  if (draw) {
    draw.destroy()
    draw.clear()
    draw = null
  }
})
</script>

<style lang="scss" scoped></style>
